//
//  YCGoCommentTableViewCell.m
//  lianganyuchu
//
//  Created by iMac on 17/2/27.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCGoCommentTableViewCell.h"
#define IMG_SIZE 60
#define BtnHeight 18

@interface YCGoCommentTableViewCell ()
{
    CWStarRateView *_starRateView;
    CommentTable *_comment;
}
@end
@implementation YCGoCommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.btnCancel.layer.borderColor=MAIN_COLOR.CGColor;
    self.btnCancel.layer.borderWidth=1.0f;
    self.btnCancel.layer.cornerRadius=2.0f;
    [self.btnCancel.titleLabel setFont:[UIFont LightFontOfSize:14]];
    
    self.btnReply.layer.cornerRadius=2.0f;
    [self.btnReply.titleLabel setFont:[UIFont LightFontOfSize:14]];
    
    [self.labComment setFont:[UIFont LightFontOfSize:15]];
    [self.labTime setFont:[UIFont LightFontOfSize:12]];
    [self.labTime setTextColor:TEXT_LIGHT];
    
    
    [self.textView setFont:[UIFont LightFontOfSize:15]];
    [self.textView setTextColor:TEXT_MIDDLE];
    UIView * keyboardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, 50)];
    [keyboardView setBackgroundColor:BACK_COLOR];
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(kMainScreen_Width-50-kDistance, 0, 50, 50)];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    [button setTitleColor:BLUE_COLOR forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont LightFontOfSize:17]];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [keyboardView addSubview:button];
    self.textView.inputAccessoryView =keyboardView;
    self.textView.delegate=self;
}
-(void)buttonClicked:(id)sender
{
    [self.textView resignFirstResponder];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)btnCancelClicked:(id)sender {
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (IBAction)btnReplyClicked:(id)sender {
    if (self.replyBlock) {
        self.replyBlock(self.textView.text);
    }
}

-(void)bindWith:(CommentTable *)comment
{
    _comment=comment;
    if (_starRateView==nil) {
        _starRateView = [[CWStarRateView alloc] initWithFrame:CGRectMake(45, 12, 100, 15) numberOfStars:5];
        _starRateView.allowIncompleteStar = YES;
        _starRateView.hasAnimation = YES;
        _starRateView.userInteractionEnabled=NO;
    }
    CGFloat prepent=comment.shop_score.floatValue/5;
    _starRateView.scorePercent = prepent;
    [self.backView addSubview:_starRateView];
    
     CGFloat textHeight=getTextSize(comment.user_content, kMainScreen_Width-60, [UIFont LightFontOfSize:15]).height-20;
    
    [[self.buttonView subviews]
     makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat tagHeight=0;
    if (comment.tags_content.length>0) {
        CGFloat w = 0;
        CGFloat h = 15;
        NSArray *remarks=[comment.tags_content componentsSeparatedByString:@","];
        for (int i=0; i<remarks.count; i++) {
            UILabel *label=[[UILabel alloc]init];
            NSString *title=remarks[i];
            CGFloat width=getTextSize(title, kMainScreen_Width, [UIFont LightFontOfSize:12]).width+20;
            if (w+width>kMainScreen_Width-60) {
                w=0;
                h=h+BtnHeight+10;
            }
            [label setFrame:CGRectMake(w, h, width, BtnHeight)];
            w = w+width+10;
            [label setText:title];
            [label setTextColor:TEXT_LIGHT];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label setFont:[UIFont LightFontOfSize:12]];
            label.layer.borderWidth=0.5f;
            label.layer.borderColor=LINE_COLOR.CGColor;
            label.layer.cornerRadius=10.0f;
            [self.buttonView addSubview:label];
        }
        tagHeight=h+BtnHeight;
    }

    
    if (comment.images.count>0) {
         self.buttonViewTopConstraint.constant=75.0f;
         [self.backView addShadowWith:CGSizeMake(kMainScreen_Width-2*kDistance, 315+tagHeight+textHeight)];
        
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for (int i=0; i<comment.images.count; i++) {
            UIImageView *imageView=[[UIImageView alloc]init];
            [imageView setFrame:CGRectMake(i*IMG_SIZE+i*kDistance, 0, IMG_SIZE, IMG_SIZE)];
            [imageView load:comment.images[i]];
            imageView.tag=i;
            [imageView click:self action:@selector(clickImage:)];
            [self.scrollView addSubview:imageView];
        }
    }else{
         self.buttonViewTopConstraint.constant=CGFLOAT_MIN;
         [self.backView addShadowWith:CGSizeMake(kMainScreen_Width-2*kDistance, 245+tagHeight+textHeight)];
         [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    [self.labComment setText:comment.user_content];
    [self.labTime setText:comment.created_at];
    
    [self.textView setText:comment.replyText];
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    _comment.replyText=textView.text;
    return YES;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [textView setInputView:nil];
    
    UITableView *tableView=(UITableView *)self.superview.superview;
    UIView *view = self.superview.superview.superview;
    
    CGRect rect = [self convertRect:self.frame toView:view];
    if (rect.origin.y / 2 + rect.size.height>=kMainScreen_Height-216) {
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);
        [tableView scrollToRowAtIndexPath:[tableView indexPathForCell:self] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
        
    }
    
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if (textView.text>0) {
        [self.labPlaceHolder setHidden:YES];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    UITableView * tableView=(UITableView *)[self  superview];
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.30f];
    tableView.center = CGPointMake(kMainScreen_Width/2, H(tableView)/2);
    [UIView commitAnimations];
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if (text.length>0) {
        [self.labPlaceHolder setHidden:YES];
    }
    if (textView.text.length==0) {
        [self.labPlaceHolder setHidden:NO];
    }
    return YES;
}

-(void)clickImage:(UIGestureRecognizer *)gesture
{
    YLPhotoBrower *brower=[[YLPhotoBrower alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    brower.currentIndex=gesture.view.tag;
    brower.photos=[NSMutableArray arrayWithArray:_comment.images];
    [brower reloadDatasWith:YES];
    [brower hideDeleteBtn];
    [brower show];
    
}

@end




